/**
 * Created by lyf on 2017/6/19.
 */
'use strict'

let node_ssh = require('node-ssh');
let deployInfo = require('../lib/excel');
const serviceConfig = require('../conf/service.json');
const xlsx = require ('node-xlsx');
const fs = require('fs');
const USER = serviceConfig.user;
const PASSWORD = serviceConfig.password;
const serviceInfo = {};

module.exports =  function getServiceInfo(){
    return new Promise (async function(resolve, reject){
        for ( let i = 0; i < deployInfo.length; i++){
            if(deployInfo[i][1]!= 'redis'  && deployInfo[i][1] != 'kongRedis' && deployInfo[i][1] != 'slb'){
                console.log(deployInfo[i][1]);
                let ssh = new node_ssh();
                const host = deployInfo[i][0].replace(/(^s*)|(s*$)/g, "");
                deployInfo[i][0] = host;
                console.log(host);
                await ssh.connect({
                    host: host,
                    username: USER ,
                    password: PASSWORD
                });
                //ssh.execCommand ('docker rm -f $(docker ps -a -q)');
                const result = await ssh.execCommand ( "ifconfig eth0 | grep inet| grep -v inet6 | tr -d \"addr:\" | awk '{print $2}' ");
                deployInfo[i].unshift (result.stdout);
                ssh.dispose();
            }else{
                deployInfo[i].unshift(deployInfo[i][0]);
            }
            serviceInfo[deployInfo[i][2]] = serviceInfo[deployInfo[i][2]] || [];
            serviceInfo[deployInfo[i][2]].push(deployInfo[i]);

        }
        deployInfo.unshift (["inner","outer","service"]);
        var buffer = xlsx.build([{name: "mySheetName", data: deployInfo}]);
        fs.writeFileSync (`${__dirname}/../result/deploy_result.xlsx` , buffer);
        resolve(serviceInfo);
    })


};

